package com.hunan.api.catv.mq.consumer;

import com.google.gson.JsonObject;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.member.sign.MemberSigninRecordBiz;
import com.hunan.api.catv.service.biz.system.SystemDictBiz;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 描述
 * @date 2021/2/1 0001
 * @package com.hunan.api.catv.mq.consumer
 * @company 湖南有线
 */
@Component
public class TvAccountBindChangeEventConsumer {

    public static final Logger log = LoggerFactory.getLogger(TvAccountBindChangeEventConsumer.class);
    private static final String TV_ACCESS_TOKEN = "TV::accessToken";
    private static final String TV_MEMBER_START_MOD = "TV::memberStartMod.";
    @Autowired
    RedisUtils redisUtils;

    @Autowired
    SystemDictBiz systemDictBiz;
    @Autowired
    MemberSigninRecordBiz memberSigninRecordBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.CA_BIND_CHANGE_QUEUE, durable = "true"), exchange = @Exchange(value = MqConstants.CA_BIND_CHANGE_EXCHANGE, durable = "true", type = "topic"), key = MqConstants.CA_BIND_CHANGE_ROUTINGKEY))
    public void task(String msg) {
        log.info("获取修改绑定大屏账号事件,{}:" + msg);
        try {
            JsonObject msgJson = GsonUtils.fromJson(msg, JsonObject.class);
            // 0：解绑 1：绑定
            Integer type = msgJson.get("type").getAsInt();
            Integer memberId = msgJson.get("memberId").getAsInt();
            if (type == 0) {
                redisUtils.hdel(TV_ACCESS_TOKEN, String.valueOf(memberId));
                redisUtils.del(TV_MEMBER_START_MOD + memberId);
            }
        } catch (Exception e) {
            log.error("修改绑定大屏账号失败：{}", e);
        }
    }

}
